import 'package:ddstore/dd_notifier.dart';
import 'package:flutter/material.dart';

class NotifierTestPage extends StatefulWidget {
  const NotifierTestPage({super.key});

  @override
  State<NotifierTestPage> createState() => _NotifierTestPageState();
}

class _NotifierTestPageState extends State<NotifierTestPage> {
  late DDNotifier _notifier;

  @override
  void initState() {
    super.initState();
    _notifier = DDNotifier();
    _notifier.addListener(
      name: "test1",
      callback: () {
        debugPrint("处罚test1事件");
      },
    );
    _notifier.addListener(
      name: "test2",
      callback: (int v1, int v2, {required String name}) {
        debugPrint("回调的值是v1:$v1, v2:$v2, name:$name");
      },
    );
    _notifier.addListener(
      name: "test3",
      callback: ({String? name1, required String name2}) {
        debugPrint("回调的值 name1:$name1, name2:$name2");
      },
    );
    _notifier.addListener(
      name: "test4",
      callback: () {
        debugPrint("收到test4事件");
        return "123";
      },
    );
    _notifier.addListener(
      name: "test5",
      callback: () async {
        debugPrint("收到test5事件2s后回调");
        await Future.delayed(const Duration(seconds: 2));
        return "123";
      },
    );
    _notifier.addListener(
        name: "test5",
        callback: () async {
          debugPrint("收到test5事件,5s后回调");
          await Future.delayed(const Duration(seconds: 5));
          return "456";
        });
    _notifier.addListener(
        name: "test5",
        callback: () {
          debugPrint("收到test5事件，无返回值");
        });
    _notifier.addListener(
        name: "test6",
        callback: () {
          debugPrint("收到test6事件，无返回值");
          _notifier.dispatch("test5", type: DDActionFutureType.wait);
        });
    _notifier.addListener(
        name: "test8",
        callback: () {
          debugPrint("收到test8事件，无返回值");
          _notifier.dispatch("test8", type: DDActionFutureType.wait);
        });
    _notifier.addListener(
        name: "test9",
        callback: () {
          debugPrint("收到test9事件，无返回值");
          _notifier.addListener(
              name: "test5",
              callback: () {
                debugPrint("新增test5事件");
              });
        });
    _notifier.addListener(name: "test10", callback: test10);
    _notifier.addListener(name: "test11", callback: test11);
    _notifier.addListener(
        name: "test12",
        callback: () {
          debugPrint("收到test12事件");
          _notifier.dispose();
        });
    _notifier.addListener(name: "test13", callback: test13);
  }

  void test10() {
    debugPrint("收到test10事件");
    _notifier.addListener(name: "test10", callback: test10);
  }

  void test11() {
    debugPrint("收到test11事件");
    _notifier.removeListener(name: "test11", callback: test11);
  }

  void test13() {
    debugPrint("收到test11事件");
    _notifier.addListener(name: "test11", callback: test11);
    _notifier.removeListener(name: "test10", callback: test10);
    _notifier.dispatch("test5");
  }

  @override
  void dispose() {
    _notifier.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("我是一个store测试页面"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            TextButton(
              onPressed: () {
                _notifier.dispatch("test1");
              },
              child: Text("test1事件出发，无参数"),
            ),
            TextButton(
              onPressed: () {
                _notifier.dispatch("test1", positionalArguments: [1]);
              },
              child: Text("test1事件出发，有一个参数，会报错"),
            ),
            TextButton(
              onPressed: () {
                _notifier.dispatch("test2",
                    positionalArguments: [1, 2],
                    namedArguments: {#name: "name"});
              },
              child: Text("test2事件出发，存在三个参数"),
            ),
            TextButton(
              onPressed: () {
                _notifier.dispatch("test3",
                    namedArguments: {#name1: "n1", #name2: "n2"});
              },
              child: Text("test3事件出发，存在一个可选参数，一个必选参数"),
            ),
            TextButton(
              onPressed: () {
                final result = _notifier.dispatch(
                  "test4",
                );
                debugPrint("收到的返回参数：$result");
              },
              child: Text("带返回参数的test4事件"),
            ),
            TextButton(
              onPressed: () async {
                final result = await _notifier.dispatch("test5",
                    type: DDActionFutureType.wait);
                debugPrint("收到的返回参数：$result");
              },
              child: Text("test5 异步事件, wait操作"),
            ),
            TextButton(
              onPressed: () async {
                final result = await _notifier.dispatch("test5",
                    type: DDActionFutureType.any);
                debugPrint("收到的返回参数：$result");
              },
              child: Text("test5 异步事件, any操作"),
            ),
            TextButton(
              onPressed: () async {
                final result = await _notifier.dispatch("test5",
                    type: DDActionFutureType.none);
                debugPrint("收到的返回参数：$result");
              },
              child: Text("test5 异步事件, none操作"),
            ),
            TextButton(
                onPressed: () {
                  _notifier.dispatch("test6");
                },
                child: Text("test6 嵌套dispatch")),
            TextButton(
                onPressed: () {
                  _notifier.dispatch("test7");
                },
                child: Text("test7空事件")),
            TextButton(
                onPressed: () {
                  _notifier.dispatch("test8");
                },
                child: Text("test8同名事件，避免死循环")),
            TextButton(
                onPressed: () {
                  _notifier.dispatch("test9");
                },
                child: Text("test9监听内，添加新的监听")),
            TextButton(
                onPressed: () {
                  _notifier.dispatch("test10");
                },
                child: Text("test10,监听内覆盖监听")),
            TextButton(
                onPressed: () {
                  _notifier.dispatch("test11");
                },
                child: Text("test11,监听内移除监听")),
            TextButton(
                onPressed: () {
                  _notifier.dispatch("test12");
                },
                child: Text("test12,监听内dispose")),
            TextButton(
                onPressed: () {
                  _notifier.dispatch("test13");
                },
                child: Text("test13,监听内组合监听和移除")),
          ],
        ),
      ),
    );
  }
}
